package com.example.messagemodule.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.messagemodule.R;
import com.example.messagemodule.adapter.InquiryHintAdapter;
import com.example.messagemodule.entity.DoctorWorkTabEntity;
import com.yilijk.base.utils.CommonUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by SHICHENG
 * <p>
 * Time on
 */
public class InquiryHintView extends LinearLayout {
    View view;
    RecyclerView inquiryHintRv;
    LinearLayout inquiryHintLl;
    TextView inquiryHintTitle;
    ImageView inquiryHintIv;
    List<DoctorWorkTabEntity.DataBean> data;
    InquiryHintAdapter inquiryHintAdapter;
    InquiryHintClickListener inquiryHintClickListener;

    public InquiryHintView(Context context) {
        super(context);
        init(context);
    }

    public InquiryHintView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public InquiryHintView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    public InquiryHintView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init(context);
    }

    private void init(Context context){
        view = LayoutInflater.from(context).inflate(R.layout.view_inquirt_hint, this);
        inquiryHintRv = view.findViewById(R.id.inquiry_hint_rv);
        inquiryHintLl = view.findViewById(R.id.inquiry_hint_ll);
        inquiryHintTitle = view.findViewById(R.id.inquiry_hint_title);
        inquiryHintIv = view.findViewById(R.id.inquiry_hint_iv);
        data = new ArrayList<>();
        inquiryHintAdapter = new InquiryHintAdapter(R.layout.item_inquiry_hint, data);
        inquiryHintRv.setLayoutManager(new GridLayoutManager(getContext(),4));
        inquiryHintRv.setAdapter(inquiryHintAdapter);
        inquiryHintAdapter.notifyDataSetChanged();
        inquiryHintIv.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                inquiryHintLl.setVisibility(GONE);
            }
        });
        inquiryHintAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (data.get(position).isEnable()){
                    if (inquiryHintClickListener!=null){
                        inquiryHintClickListener.clickAdapter(position);
                    }
                }else{
                    //无问诊权限
                }

            }
        });
    }




    public void setData(List<DoctorWorkTabEntity.DataBean> data, String chargeStatus){
        if (data.size() == 0){
            //均未开通不展示
            return;
        }

        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).isEnable()){
                //如果有一个开通 展示UI 结束遍历   如果都未开通 则不做处理
                extracted(data, chargeStatus);
                break;
            }
        }
    }

    private void extracted(List<DoctorWorkTabEntity.DataBean> data, String chargeStatus) {
        inquiryHintLl.setVisibility(GONE);
        if ("end".equals(chargeStatus) || "escCharge".equals(chargeStatus)) {
            inquiryHintTitle.setText("问诊已结束");
            inquiryHintLl.setVisibility(VISIBLE);
        } else if ("followupend".equals(chargeStatus)) {
            inquiryHintTitle.setText("随访已结束");
            inquiryHintLl.setVisibility(VISIBLE);
        } else if ("queryResidueend".equals(chargeStatus)) {
            inquiryHintTitle.setText("咨询已结束");
            inquiryHintLl.setVisibility(VISIBLE);
        }
        //通过tabcode 设置icon
        for (int i = 0; i < data.size(); i++) {
            switch (data.get(i).getTabCode()){
                case "textCharges"://图文
                    data.get(i).setUnSelectIcon(CommonUtils.imageTranslateUri(getContext(),R.mipmap.img_text_charges_0));
                    data.get(i).setSelectIcon(CommonUtils.imageTranslateUri(getContext(),R.mipmap.img_text_charges_1));
                    break;
                case "phoneCharges"://电话
                    data.get(i).setUnSelectIcon(CommonUtils.imageTranslateUri(getContext(),R.mipmap.img_phone_charges_0));
                    data.get(i).setSelectIcon(CommonUtils.imageTranslateUri(getContext(),R.mipmap.img_phone_charges_1));
                    break;
                case "videoCharges"://视频
                    data.get(i).setUnSelectIcon(CommonUtils.imageTranslateUri(getContext(),R.mipmap.img_video_charges_0));
                    data.get(i).setSelectIcon(CommonUtils.imageTranslateUri(getContext(),R.mipmap.img_video_charges_1));
                    break;
                case "etp"://购药
                    data.get(i).setUnSelectIcon(CommonUtils.imageTranslateUri(getContext(),R.mipmap.img_etp_0));
                    data.get(i).setSelectIcon(CommonUtils.imageTranslateUri(getContext(),R.mipmap.img_etp_1));
                    break;
            }
        }
        this.data.clear();
        this.data.addAll(data);
        inquiryHintAdapter.notifyDataSetChanged();
    }

    public void setInquiryHintClickListener(InquiryHintClickListener inquiryHintClickListener) {
        this.inquiryHintClickListener = inquiryHintClickListener;
    }

    public interface InquiryHintClickListener{
        void clickAdapter(int position);
    }

}
